package com.bank.china.loan.controller;


import com.bank.china.loan.entity.DepartmentDO;
import com.bank.china.loan.entity.EmployeeDO;
import com.bank.china.loan.service.DepartmentService;
import com.bank.china.loan.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author christopher
 * @since 2021-09-30
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    private Logger log = LoggerFactory.getLogger(EmployeeController.class);

    @GetMapping("login")
    public String login(){
        return "employee/login";
    }


    @GetMapping("add_employee")
    public String addEmployee(Model model){
        List<DepartmentDO> departs = departmentService.list();
        model.addAttribute("departs",departs);

        return "employee/add_emp";
    }

    @GetMapping("do_add_emp")
    public String doAddEmp(EmployeeDO employee){
        log.info("log{}",employee);
        employee.setPassword("1111");
        employeeService.save(employee);
        return "redirect:/employee/show_employee";
    }


    @PostMapping("/do_login")
    public String doLogin(EmployeeDO employee, HttpSession session, Model model){
        QueryWrapper<EmployeeDO> employeeDOQueryWrapper = new QueryWrapper<>(employee);
        EmployeeDO loginEmployee = employeeService.getOne(employeeDOQueryWrapper);

        if (null != loginEmployee){
            session.setAttribute("user",loginEmployee);
            return "home";
        }
        model.addAttribute("msg","用户名密码错");
        return "employee/login";

    }

    @GetMapping("show_employee")
    public String showEmployee(Model model){
        List<EmployeeDO> employees = employeeService.selectEmployeeAll();
        model.addAttribute("emps",employees);
        return "employee/show_employee";
    }

    @GetMapping("show_employee/{id}")
    public String showEmployeeBYId(@PathVariable Integer id,Model model){
        EmployeeDO employee = employeeService.selectEmployeeById(id);
        model.addAttribute("emp",employee);
        return "/employee/show_emp_info";
    }

    @GetMapping("update_employee/{id}")
    public String updateEmployeeBYId(@PathVariable Integer id,Model model){
        EmployeeDO employee = employeeService.selectEmployeeById(id);
        model.addAttribute("emp",employee);
        List<DepartmentDO> departs = departmentService.list();
        model.addAttribute("departs",departs);
        return "/employee/update_emp_info";
    }

    @GetMapping("do_update_emp")
    public String doUpdateEmployee(EmployeeDO employee){
        log.info("log{}",employee);
        employeeService.updateById(employee);

        return "redirect:/employee/show_employee";
    }


    @GetMapping("del_emp/{id}")
    public String delEmployee(@PathVariable Integer id){
        boolean b = employeeService.removeById(id);
        if(b){
            return "redirect:/employee/show_employee";
        }
        return "home";
    }


}

